package com.eos.orm.c3p0.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.sql.*;

import com.eos.common.tool.CoreKeyContext;
import com.eos.orm.c3p0.dao.AbstractC3p0Dao;
import com.eos.orm.c3p0.dao.DBConnectionManager;
import com.eos.orm.mybatis.model.ProductModel;
import com.hf.utils.StringUtil;

/**
 * product_model产品型号的分页查询及相关批处理操作
 * 
 * @author sz
 * 
 */
public class ProductModelDaoImpl extends AbstractC3p0Dao<ProductModel> {

	/**
	 * 查询的条件：id/insert_user_id/update_user_id/model_name/deal_flag/status/brand_id/product_id
	 */
	public String condition1(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();
		if (null == map)
			return condition.append("1=0").toString();
		if (!StringUtil.isEmpty(map.get("id")))
			condition.append("id='").append(map.get("id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("insert_user_id")))
			condition.append("insert_user_id='").append(
					map.get("insert_user_id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("update_user_id")))
			condition.append("update_user_id='").append(
					map.get("update_user_id")).append("' and ");
		if (!StringUtil.isEmpty(map.get("product_id")))
			condition.append("product_id='").append(map.get("product_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("brand_id")))
			condition.append("brand_id='").append(map.get("brand_id"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("model_name")))
			condition.append("model_name='").append(map.get("model_name"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("deal_flag")))
			condition.append("deal_flag='").append(map.get("deal_flag"))
					.append("' and ");
		if (!StringUtil.isEmpty(map.get("status")))
			condition.append("status=").append(map.get("status")).append(
					" and ");
		return condition.append("1=1").toString();
	}

	@Override
	public List<ProductModel> findPage(Map<String, String> map) {
		List<ProductModel> templist = new ArrayList<ProductModel>();
		if (null == map)
			return templist;
		Connection connection = DBConnectionManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = StringUtil.replace(StringUtil.replace(StringUtil.replace(
				CoreKeyContext.getCoreKey("tabs_page_sqlscript"),
				"$table_name$", map.get("table_name")), "$condition1$",
				condition1(map)), "$condition2$", condition2(map));
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ProductModel temp = new ProductModel();
				temp.setId(rs.getInt("id"));
				temp.setInsertUserId(rs.getString("insert_user_id"));
				temp.setUpdateUserId(rs.getString("update_user_id"));
				temp.setInsertTime(rs.getString("insert_time"));
				temp.setModifyTime(rs.getString("modify_time"));
				temp.setProductId(rs.getInt("product_id"));
				temp.setBrandId(rs.getInt("brand_id"));
				temp.setModelName(rs.getString("model_name"));
				temp.setModelDesc(rs.getString("model_desc"));
				temp.setStatus(rs.getString("status"));
				temp.setDealFlag(rs.getString("deal_flag"));
				templist.add(temp);
				temp = null;
			}
		} catch (SQLException e) {
			System.out
					.println("product_model exec findPage operator is fail,Exception is :"
							+ e.getMessage());
		} finally {
			freeResource(connection, pstmt, rs);
		}
		return templist;
	}
}
